<?php
namespace app\model;

use think\Model;

function merge_spaces($string)
{
    return preg_replace("/\s(?=\s)/", "\\1", $string);
}

class Note extends Model
{
    protected $table='note';

    public function findAll()
    {
        return Note::select()->toArray();
    }
    public function pageFindAll($page)
    {
        $pageNo = $page['pageNo'];
        $pageSize = $page['pageSize'];
        if ($pageNo<=0) {
            $pageNo=0;
        }
        if ($pageSize<=0) {
            $pageSize=10;
        }
        $result = Note::$db->query("select count(*) from note ");
        $count= $result[0]['count(*)'];
        $list= Note::$db->query('select * from note limit '.$pageNo*$pageSize.','.$pageSize);
        $data=['page'=>[
            'pageSize'=>$pageSize,
            'pageNo'=>$pageNo,
            'count'=>$count,
            ],
            'result'=>$list
        ];
        return $data;
    }
    public function pageFindAll2($page)
    {
        $pageNo = $page['pageNo'];
        $pageSize = $page['pageSize'];
        if ($pageNo<=0) {
            $pageNo=0;
        }
        if ($pageSize<=0) {
            $pageSize=10;
        }
        $like=" ";
        if (isset($page['search'])) {
            $search=merge_spaces($page['search']);
            $search=explode(" ", $search);
            $like=" and";
            $arrCount = count($search);
            for ($i = 0; $i < $arrCount; $i++) {
                $item= $search[$i];
                $like = $like." title like '%".$item."%' or tips like '%".$item."%' or content like '%".$item."%' ";
                if ($i==$arrCount-1) {
                    $like = $like." ";
                } else {
                    $like = $like." or ";
                }
            }
            // var_dump($like);
            // exit();
            // $like="  title LIKE '[".$item."]' ";
        }
        $where="where bState = '1' ";
        $orderBy='ORDER BY updateTime asc,readNumber desc';
        $result = Note::$db->query("select count(*) from note ".$where.$like);
        $count= $result[0]['count(*)'];
        // var_dump('select * from note '.$where.$like.$orderBy.' limit '.$pageNo*$pageSize.','.$pageSize);
        // exit();
        $list= Note::$db->query('select * from note '.$where.$like.$orderBy.' limit '.$pageNo*$pageSize.','.$pageSize);
        $data=['page'=>[
            'pageSize'=>$pageSize,
            'pageNo'=>$pageNo,
            'count'=> $count,
            ],
            'result'=>$list
        ];
        return $data;
    }
    public function add($data)
    {
        return Note::replace()->insert($data);
    }
    public function findById($id)
    {
        $data = Note::where(['noteId'=>$id])->find();
        return $data;
    }

    public function updateNote($data)
    {
        try {
            $Note =Note::where(['noteId'=>$data['noteId']])->find();
            $Note['title']= $data['title'];
            $Note['content']= $data['content'];
            $Note['type']= $data['type'];
            $Note['reprintName']= $data['reprintName'];
            $Note['reprintUrl']= $data['reprintUrl'];
            $Note['tips']= $data['tips'];
            $Note['updateTime']= $data['updateTime'];
            $Note['bState']= $data['bState'];
            return $Note->save();
        } catch (\Throwable $th) {
            throw $th;
        }
        return false;
    }

    public function hotPageList($page)
    {
        $pageNo = $page['pageNo'];
        $pageSize = $page['pageSize'];
        if ($pageNo<=0) {
            $pageNo=0;
        }
        if ($pageSize<=0) {
            $pageSize=10;
        }
        $list= Note::$db->query("select * from note where bState='1' order by readNumber desc , updateTime desc , createTime desc   limit ".$pageNo*$pageSize.','.$pageSize);
        $data=['page'=>[
            'pageSize'=>$pageSize,
            'pageNo'=>$pageNo,
            'count'=>count($list),
            ],
            'result'=>$list
        ];
        return $data;
    }
    public function deleteNote($noteId)
    {
        Note::where(['noteId'=>$noteId])->delete();
        return true;
    }

    public function randList(Type $var = null)
    {
        $list= Note::$db->query("select * from note where noteId >= (SELECT floor(RAND() * (SELECT MAX(noteId) FROM `note`))) and bState = '1' ORDER BY noteId LIMIT 5");
        $data=[
            'result'=>$list
        ];
        return $data;
    }
}
